﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
using System.Diagnostics;
using System.IO;
using ServEvo.Layout.Interfaces;
using ServEvo.Layout.Enum;
using ServEvo.ServConfig;

namespace ServEvo.ServControl
{
    class ApacheManager : AdminManager, IService, IAdminService, IServeur
    {
        private bool _isServiceInstalled;
        public bool IsServiceInstalled
        {
            get { return _isServiceInstalled; }
        }
        //bool IAdminService.IsServiceInstalled
        //{
        //    get { return _isServiceInstalled; }
        //}

        public ApacheManager(string _installDir, string _configFileLocation, string _processLocation)
        {
            oLock = new object();
            sInstallDir = _installDir;
            _isServiceInstalled = false;
            _listServices = new List<Service>() { Service.HTTP };
            initList();
            _listProcess = new Dictionary<Service, Process>();
            _listProcessStartInfo = new Dictionary<Service, ProcessStartInfo>();
            _listProcessId = new Dictionary<Service, int>();
            string sFullInstallDir = string.Empty;
            if (sInstallDir.Substring(sInstallDir.Length - 1, 1) != @"\") sInstallDir += @"\";

            if (!sInstallDir[1].Equals(':'))
                sFullInstallDir = System.Windows.Forms.Application.StartupPath + sInstallDir;
            else
                sFullInstallDir = sInstallDir;

            _listProcessStartInfo[Service.HTTP] = new ProcessStartInfo(sFullInstallDir + _processLocation);
            _listProcessStartInfo[Service.HTTP].UseShellExecute = false;
            _listProcessStartInfo[Service.HTTP].RedirectStandardOutput = true;
            _listProcessStartInfo[Service.HTTP].CreateNoWindow = true;
            _listProcessStartInfo[Service.HTTP].WindowStyle = ProcessWindowStyle.Hidden;

             _listServiceController[Service.HTTP] = new ServiceController();

            try
            {
                _listServiceController[Service.HTTP] = new ServiceController("ServEvo HTTP");
                if (_listServiceController[Service.HTTP].Status == ServiceControllerStatus.Running)
                {
                    bool t = true;
                }
                _isServiceInstalled = true;
            }
            catch (InvalidOperationException) { }
            catch (ArgumentException) { }
        }

        public override string ToString()
        {
            return "Apache (Build-In)";
        }

        List<Service> IServeur.getListServeurs()
        {
            return _listServices;
        }
        List<Service> IService.getListServices()
        {
            return _listServices;
        }

        public static ApacheManager checkConfiguration(String sInstallDir, IniFile.IniSection configHTTP)
        {
            string sFullInstallDir = String.Empty;
            string apacheVersion = configHTTP.GetKey("Version").Value.Trim();
            string apacheInstallDir = configHTTP.GetKey("InstallDir").Value.Trim();
            string apacheConfigFile = configHTTP.GetKey("ConfigFile").Value.Trim();
            string apacheProcessFile = configHTTP.GetKey("ProcessFile").Value.Trim();

            if (sInstallDir.Substring(sInstallDir.Length - 1, 1) != @"\") sInstallDir += @"\";

            if (!sInstallDir[1].Equals(':'))
                sFullInstallDir = System.Windows.Forms.Application.StartupPath + sInstallDir;
            else
                sFullInstallDir = sInstallDir;

            if (!String.IsNullOrEmpty(apacheVersion)
                && !String.IsNullOrEmpty(apacheInstallDir)
                && !String.IsNullOrEmpty(apacheConfigFile)
                && !String.IsNullOrEmpty(apacheProcessFile))
            {
                if (Directory.Exists(sFullInstallDir + apacheInstallDir))
                {
                    if (File.Exists(sFullInstallDir + apacheInstallDir + apacheVersion + "\\" + apacheConfigFile)
                        && File.Exists(sFullInstallDir + apacheInstallDir + apacheVersion + "\\" + apacheProcessFile))
                    {
                        return new ApacheManager(sInstallDir + apacheInstallDir + apacheVersion, apacheConfigFile, apacheProcessFile);
                    }
                }
            }
            return null;
        }

    }
}
